<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:36
 */
namespace Model;
abstract class Mod
{
	protected $_table = '';  //数据表名字

	/**
	 * 插入新数据
	 * @param array $_data
	 * @return mixed
	 */
	public function insert(Array $_data)
	{
		return pdo_insert($this->_table,$_data);
	}

	public function findOne(Array $_where = array(), Array $_return = array())
	{
		return pdo_get($this->_table,$_where,$_return);
	}

	public function findAll(Array $_where, Array $_other = array(), Array $_return = array())
	{
		return $this->selectAll($_where,$_other,$_return);
	}

	public function update(Array $_data,Array $_where)
	{
		return pdo_update($this->_table,$_data,$_where);
	}

	public function delete(Array $_where)
	{
		return pdo_delete($this->_table,$_where);
	}

	/**
	 * 获取所有数据
	 * @param array $_where  条件
	 * @param array $_other  order||limit
	 * @param array $_return  返回字段
	 * @return mixed
	 */
	protected function selectAll(Array $_where = array(),Array $_other = array(),Array $_return = array()){
		$_param = '';
		$_returnStr = '*';
		$_otherStr = '';
		if(count($_where) > 0) $_param = $this->toWhere($_where);
		if(count($_return) > 0){
			$_returnStr = '';
			foreach($_return as $_v){
				$_returnStr .= $_v.',';
			}
			$_returnStr = substr($_returnStr,0,-1);
		}
		if(count($_other) > 0){
			if(isset($_other['order'])){
				$_otherStr .= 'ORDER BY ';
				$_orderCount = count($_other['order']) / 2;
				for($i=0;$i<=$_orderCount;$i+=2){
					$_otherStr .= $_other['order'][$i].' '.strtoupper($_other['order'][$i+1]).',';
				}
				$_otherStr = substr($_otherStr,0,-1);
			}
			if(isset($_other['limit']) && count($_other['limit'])>0){
				$_otherStr .= ' LIMIT '.$_other['limit'][0].','.$_other['limit'][1];
			}
		}
		$_sql = "SELECT 
						$_returnStr 
				  FROM
				  		".tablename($this->_table)."
				  		$_param[0]
				  		$_otherStr
				 ";
		return pdo_fetchall($_sql,$_param[1]);
	}

	/**
	 * 运算
	 * @param array $_data
	 * @param array $_where
	 * @return mixed
	 */
	public function math(Array $_data,Array $_where){
		$_tempArr = $this->arrToMath($_data,$_where);
		$_sql = "UPDATE
    					".tablename($this->_table)."
    			 SET
						$_tempArr[0]
				        $_tempArr[1]
    			";
		return pdo_query($_sql,$_tempArr[2]);
	}

	/**
	 * 查询总数
	 * @param array $_where 条件
	 * @param string $_field  count(字段)
	 * @return mixed
	 */
	public function total(Array $_where = array(),$_field = 'id'){
		$_whereStr = '';
		if(count($_where) > 0){
			$_whereStr = $this->toWhere($_where);
		}
		$_sql = "SELECT 
						count($_field) 
					FROM
						".tablename($this->_table)."
					$_whereStr[0]
				";
		return pdo_fetchcolumn($_sql,$_whereStr[1]);
	}

	/**
	 * 获取某个字段总和
	 * @param array $_where
	 * @param $_field
	 */
	public function sum(Array $_where = array(),$_field)
	{
		$_whereStr = '';
		if(count($_where) > 0){
			$_whereStr = $this->toWhere($_where);
		}
		$_sql = "SELECT 
						SUM($_field)
					FROM
						".tablename($this->_table)."
					$_whereStr[0]
				";
		$_sum = pdo_fetchcolumn($_sql,$_whereStr[1]);
		return $_sum ? $_sum : 0;
	}

	/**
	 * 返回where参数绑定字符串 uniacid=:uniacid
	 * @param array $_where
	 * @return bool|string
	 */
	private function toWhere(Array $_where){
		$_param = 'WHERE ';
		$_newWhere = array();
		$_i = 0;
		foreach($_where as $_k=>$_v){
			$_arr = explode(' ',$_k);
			if(count($_arr) <= 1){
				$_newK = $_k.'_'.$_i;
				if(array_key_exists($_k,$_newWhere)) $_newK = $_k;
				$_param .= $_k.'=:'.$_newK.' AND ';
				$_newWhere[$_newK] = $_v;
			}else{
				$_newK = $_arr[0].'_'.$_i;
				if(array_key_exists($_arr[0],$_newWhere)) $_newK = $_arr[0];
				if($_arr[1] == 'like'){
                    $_param .= $_arr[0].' like :'.$_arr[0].'_'.$_i.' AND ';
                }else{
                    $_param .= $_arr[0].' '.$_arr[1].':'.$_newK.' AND ';
                }
				$_newWhere[$_newK] = $_v;
			}
			$_i++;
		}
		return array(substr($_param,0,-4),$_newWhere);
	}

	/**
	 * 字段运算
	 * @param array $_data
	 * @param array $_where
	 * @return array
	 */
	private function arrToMath(Array $_data,Array $_where){
		$_newData = '';
		$_bindParam = array();
		foreach($_data as $_k=>$_v){
			$_tempK = explode(' ',$_k);
			$_newData .= $_tempK[0] .' = '.$_tempK[0].' '.$_tempK[1].' :'.$_tempK[0].',';
			$_bindParam[$_tempK[0]] = $_v;
		}
		$_tempArr = $this->toWhere($_where);
		$_bindParam = array_merge($_bindParam,$_tempArr[1]);
		return array(substr($_newData,0,-1),$_tempArr[0],$_bindParam);
	}
}